
//分类的添加
function addCategory() {
    swal("请输入分类名称：", {
        content: "input",
        buttons: ["我点错了","确认添加"],
    }).then((value) => {
        if(value == null){
            return;
        }
        $.ajax({
            type: "post",
            url: "/admin/category/save",
            data: {"name" : value},
            success: function (result) {
                if(result.resultCode == 200){
                    swal("保存成功",{
                        icon:"success",
                    }).then(function () {
                        location.reload();
                    })
                }else{
                    swal(result.message, {
                        icon: "error",
                        button: {
                            text: "好的"
                        },
                    });
                }
            },
            error: function (result) {
                swal("保存失败", {
                    icon: "error",
                    button: {
                        text: "好的"
                    },
                });
            }
        })
    });
}

//修改分类
function updateCategory() {
    let count = 0;
    $('input[name="checkOption"]:checked').each(function () {
        count++;
    });
    if(count == 0){
        swal("请至少选择一个分类",{
            icon: "warning",
            button: {
                text: "好的"
            },
        });
        return;
    }else if(count > 1){
        swal("一次只能修改一个分类",{
           icon: "warning",
            button: {
               text: "好的"
            },
        });
        return;
    }
    let id = $('input[name="checkOption"]:checked').parent("th").parent("tr").find("[name='id']").html();
    swal("请输入修改后的分类名称：", {
        content: "input",
        buttons: ["我点错了","确认修改"],
    }).then((value) => {
        if(value == null){
            return;
        }
        $.ajax({
            type: "post",
            url: "/admin/category/update",
            data: {"id" : id , "name" : value},
            success: function (result) {
                if(result.resultCode == 200){
                    swal("修改成功",{
                        icon:"success",
                    }).then(function () {
                        location.reload();
                    })
                }else{
                    swal(result.message, {
                        icon: "error",
                        button: {
                            text: "好的"
                        },
                    });
                }
            },
            error: function (result) {
                swal("修改失败", {
                    icon: "error",
                    button: {
                        text: "好的"
                    },
                });
            }
        })
    });
}

//删除分类
function deleteCategory() {

    //定义一个数组，用来存储选中的数据id
    let ids = new Array();
    //遍历每一个名字为checkOption的复选框，其中选中的执行函数
    $('input[name="checkOption"]:checked').each(function () {
        //将选中的值id添加到数组ids中
        ids.push($(this).parent("th").parent("tr").find("[name='id']").html());
    });

    //判断ids是否为空，如果为空直接返回信息，没必要传给后台了
    if(ids.length == 0){
        swal("请至少选择一个分类删除！", {
            icon: "warning",
        });
        return;
    }

    swal({
        title: '确定删除吗？',
        text: '你将无法恢复它！',
        icon: 'warning',
        buttons: ["我在想想","确定删除"],
        dangerMode: true,
    }).then((flag) => {
        if(flag) {
            let url = "/admin/category/delete";
            $.ajax({
                type: "post",
                url: url,
                data: {"ids": ids},
                //防止深度序列化
                traditional: true,
                success: function (result) {
                    if (result.resultCode == 200) {
                        swal("删除成功", {
                            icon: "success",
                        }).then(function () {
                            /*更新完点击OK刷新页面*/
                            location.reload();
                        });
                    } else {
                        $('#categoryModal').modal('hide');
                        swal(result.message, {
                            icon: "error",
                            button: {
                                text: "好的"
                            },
                        });
                    }
                },
                error: function () {
                    swal("操作失败", {
                        icon: "error",
                        button: {
                            text: "好的"
                        },
                    });
                }
            })
        }
    })
}




